package com.xmlasia.remote;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Server {
	private static Logger logger = LoggerFactory.getLogger(Server.class);

	private final static String DEFAULT_CONTEXT_PATH = "server/server.xml";

	protected final static String[] getConfigLocations() {
		return new String[] { "classpath:" + DEFAULT_CONTEXT_PATH, };
	}

	public final static void main(String[] args) {
		Server server = new Server();
		server.start();
	}

	public void start() {
		long startTime = System.currentTimeMillis();
		ClassPathXmlApplicationContext applicationContext = null;
		try {
			logger.info("Starting SERVER application context");
			applicationContext = new ClassPathXmlApplicationContext(getConfigLocations());
			applicationContext.registerShutdownHook();
			applicationContext.start();
			logger.info("SERVER application context started");
		} catch (RuntimeException e) {
			logger.error("Fail to start up SERVER", e);
			throw e;
		}
		
		
		long endTime = System.currentTimeMillis();

		long startUpDuration = (endTime - startTime) / 1000;

		StringBuffer buffer = new StringBuffer();
		buffer.append("\n");
		buffer.append("#####################################################\n");
		buffer.append("SERVER start at " + new Date() + " using " + startUpDuration + " sec. \n");
		buffer.append("#####################################################\n");
		logger.info(buffer.toString());

		try {
			Thread.currentThread().join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
